<?php

include_once APPLICATION_PATH . "/models/Feedback.php";
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Admin_FeedbackController extends Zend_Controller_Action {
    public function init() {
       // $controller=$this->_getParam("controller","feedback");        
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model = new Model_Feedback();
        $this->modelMapper = new Model_FeedbackMapper();
        $this->user=GlobalLib::getUserLogin();
    }
    public function indexAction() {} 
    public function listAction(){}      
    public function addAction() { 
       if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."admin/feedback/list";            
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }            
            if(isset($_POST["title_feedback"])){
                $this->model->setTitle($_POST["title_feedback"]);
            } 
            if(strlen($_POST["question"])>0){                
                $this->model->setQuestion(strip_tags($_POST["question"]));
            }
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());          
            if(strlen($_POST["answer"])>0){
                $this->model->setAnswer($_POST["answer"]);
            }
            
            $this->model->setUser_Id($this->user['id']);
            $this->model->setAdmin_Id(0);
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }     
           $this->view->item= $this->model;    
    }
     public function editAction(){
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/feedback/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }        
        $this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        $getUserId=$this->model->getUser_Id();
        if(empty($getId)){
            $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(strlen($_POST["title_feedback"])>0){
                $this->model->setTitle($_POST["title_feedback"]);
            } 
            if(strlen($_POST["question"])>0){ 
                $this->model->setQuestion($_POST["question"]);
            }
            if(strlen($_POST["answer"])>0){ 
                $this->model->setAnswer(strip_tags($_POST["answer"]));
            }
            $this->model->setUser_Id($getUserId);
            $this->model->setAdmin_Id(GlobalLib::getLoginId());
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }
        $this->view->item=$this->model;
    }   
   
    public function deleteAction(){
       
        $id = $this->_getParam("id","");
        $this->view->itemm=$id;
        $redirectUrl=$this->aConfig["site"]["url"]."admin/feedback/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $item=$this->modelMapper->find($id,$this->model);
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);
        
    }     
    public function serviceAction(){
        $this->_helper->layout->disableLayout();
        foreach ($this->modelMapper-> fetchAllP() as $items ) {
            if($items->getAdmin_Id()==0){
                $admin_name="";
            } else {
                $admin_name=GlobalLib::getName('admin',$items->getAdmin_Id(),'user_name');
            }
            $menber[]=array(
                'Id'=>$items->getId(),
                'user_id'=>GlobalLib::getName('user',$items->getUser_Id(),'username'),
                'title'=>$items->getTitle(),
                'question'=>$items->getQuestion(),                
                'admin_id'=> $admin_name,
                'answer'=>$items->getAnswer()
            );
        }
        echo json_encode($menber);
        exit();
    }
    
}
